package com.zhht.checkcop.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zhht.checkcop.db.HanYuList;
import com.zhht.checkcop.db.MysqlDb;

/**
 * HttpClient工具类
 */
public class HttpsClientUtil {

    /**请求编码*/
    private static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * 执行HTTP POST请求
     * @param url url
     * @param param 参数
     * @return
     */
    public static String httpPostWithJSON(String url, Map<String, ?> param) {
        CloseableHttpClient client = null;
        try {
            if(url == null || url.trim().length() == 0){
                throw new Exception("URL is null");
            }
            HttpPost httpPost = new HttpPost(url);
            client = HttpClients.createDefault();
            if(param != null){
                StringEntity entity = new StringEntity(JSON.toJSONString(param), DEFAULT_CHARSET);//解决中文乱码问题
                entity.setContentEncoding(DEFAULT_CHARSET);
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            HttpResponse resp = client.execute(httpPost);
            if(resp.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(client);
        }
        return null;
    }

    /**
     * 执行HTTP GET请求
     * @param url url
     * @param param 参数
     * @return
     */
    public static String httpGetWithJSON(String url, Map<String, ?> param) {
        CloseableHttpClient client = null;
        try {
            if(url == null || url.trim().length() == 0){
                throw new Exception("URL is null");
            }
            client = HttpClients.createDefault();
            if(param != null){
                StringBuffer sb = new StringBuffer("?");
                for (String key : param.keySet()){
                    sb.append(key).append("=").append(param.get(key)).append("&");
                }
                url = url.concat(sb.toString());
                url = url.substring(0, url.length()-1);
            }
            HttpGet httpGet = new HttpGet(url);
            HttpResponse resp = client.execute(httpGet);
            if(resp.getStatusLine().getStatusCode() == 200) {
                return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(client);
        }
        return null;
    }

    /**
     * 关闭HTTP请求
     * @param client
     */
    private static void close(CloseableHttpClient client){
        if(client == null){
            return;
        }
        try {
            client.close();
        } catch (Exception e) {
        }
    }

    //    /**
//     * 古诗文网  扒百度汉语的诗词列表
//     * @param url
//     * @return
//     */
    public static void main(String[] args) throws Exception {
    	MysqlDb mysqldb = new MysqlDb();
    	mysqldb.connect();
    	
    	Random r = new Random(1);
    	for(int i=405;i<=13281;i++){
    		int ran1 = r.nextInt(1000);
    		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
    		try{
	    		String result = httpPostWithJSON("https://hanyu.baidu.com/hanyu/ajax/search_list?wd=%E8%AF%97%E8%AF%8D&device=pc&from=home&pn="+i+"&_="+(new Date().getTime()-ran1),null);
//	    		String result = httpPostWithJSON("https://hanyu.baidu.com/hanyu/ajax/search_list?wd=%E8%AF%97%E8%AF%8D&ptype=zici&device=pc&from=home&pn="+i+"&_="+(new Date().getTime()-ran1),null);
	    		if(StringUtils.isBlank(result)){
	    			for(int x=0;x<3;x++){
	    				int ran2 = r.nextInt(1000);
	    				int th = r.nextInt(1500);
	    				Thread.sleep(1000+th);
	    				result = httpPostWithJSON("https://hanyu.baidu.com/hanyu/ajax/search_list?wd=%E8%AF%97%E8%AF%8D&device=pc&from=home&pn="+i+"&_="+(new Date().getTime()-ran2),null);
	    				if(StringUtils.isNotBlank(result)){
	    	    			break;
	    	    		}
	    			}
	    		}
	    		Map maps = (Map)JSON.parse(result);
	    		JSONArray json = (JSONArray) maps.get("ret_array");
	    		for(int j=0;j<json.size();j++){
	    			String demo = json.get(j)+"";
	    			if(StringUtils.isNotBlank(demo)){
	    				Map mm = (Map)JSON.parse(demo);
	    				if(mm.get("body")!=null){
	    				mm.put("page", i);
	    				mm.put("body", (mm.get("body")+"").replace("'", "\""));
	    				list.add(mm);
	    				}
	    			}
	    		}
    		}catch(Exception ex){  
    			System.out.println("第"+i+"页 查询失败!");
    			ex.printStackTrace();
            }
    		
    		if(list!=null&&list.size()>0){
    			try{
    				mysqldb.berthInsert("t_baidu_shici",list);
    				int th = r.nextInt(1500);
    				Thread.sleep(1000+th);
    			}catch(Exception e){
    				System.out.println("第"+i+"页 插入失败!");
    			}
    		}
    	}
    	mysqldb.close();
    	
    }

	private static String post(String url) {
		HttpClient httpClient = null;  
         HttpPost httpPost = null;  
         String result = null;  
         try{  
             httpClient = new SSLClient();  
             httpPost = new HttpPost(url);  
             httpPost.addHeader("Content-Type", "application/json");  
//             StringEntity se = new StringEntity(JSON.toJSONString(param));  
//             se.setContentType("text/json");  
//             se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));  
//             httpPost.setEntity(se);  
             HttpResponse response = httpClient.execute(httpPost);  
             if(response != null){  
                 HttpEntity resEntity = response.getEntity();  
                 if(resEntity != null){  
                     result = EntityUtils.toString(resEntity,"utf-8");  
                 }  
             }  
         }catch(Exception ex){  
             ex.printStackTrace();  
         }
		return result;
	}
	private static String get(String url) {
		HttpClient httpClient = null;  
		HttpGet httpget = null;  
		String result = null;  
		try{  
			httpClient = new SSLClient();  
			httpget = new HttpGet(url);  
			httpget.addHeader("Content-Type", "application/json");  
//             StringEntity se = new StringEntity(JSON.toJSONString(param));  
//             se.setContentType("text/json");  
//             se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));  
//             httpPost.setEntity(se);  
			HttpResponse response = httpClient.execute(httpget);  
			if(response != null){  
				HttpEntity resEntity = response.getEntity();  
				if(resEntity != null){  
					result = EntityUtils.toString(resEntity,"utf-8");  
				}  
			}  
		}catch(Exception ex){  
			ex.printStackTrace();  
		}
		return result;
	}


}